package com.ssm.module.film.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.common.persistence.PageBean;
import com.ssm.common.service.CrudService;
import com.ssm.module.film.dao.FilmDao;
import com.ssm.module.film.entity.Film;
import com.ssm.module.film.entity.FilmQueryVo;

/**  
* <p>Title: FilmServiceImpl</p>  
* <p>Description: </p>  
* @author jzhang  
* @date 2018年6月12日 下午3:20:16  
*/
@Service
public class FilmService extends CrudService<FilmDao, Film> {
	private static final Integer pageSize = 2;
	private static final Integer firstPage = 1;
	
	@Autowired
	private FilmDao filmMapper;
	
	public List<Film> findList(FilmQueryVo filmQueryVo) throws Exception {
		return filmMapper.findList(filmQueryVo);
	}

	public String saveFilm(Film film) throws Exception {
		//判断是否是新记录
		if (film != null && film.getId() != null) {
			//更新记录
			film.preUpdate();
			filmMapper.updateSelective(film);
		} else {
			//新增记录
			film.preInsert();
			filmMapper.insertSelective(film);
		}
		return "success";
	}

	public Film selectFilmById(Film film) throws Exception {
		if (film != null && film.getId() != null) {
			film = filmMapper.selectFilmByPrimaryKey(film.getId());
		}
		return film;
	}

	public String deleteFilm(String ids) throws Exception {
		String[] strIds = ids.split(",");
		int[] intIds = new int[strIds.length];
		for (int i = 0; i < strIds.length; i++) {
			intIds[i] = Integer.valueOf(strIds[i]);
		}
		filmMapper.deleteFilm(intIds);
		return "success";
	}

	public PageBean<Film> queryFilmByCondition(FilmQueryVo filmQueryVo) {
		Integer currentPage = firstPage;
		if(filmQueryVo.getPageNo() != null){
			currentPage = Integer.valueOf(filmQueryVo.getPageNo());
		}
		if(filmQueryVo.getFilmName() != null) {
			Film film = new Film();
			film.setFilmName(filmQueryVo.getFilmName());
			filmQueryVo.setFilm(film);
		}
		
		//获取记录总数
		int count = filmMapper.rowCount(filmQueryVo);
		//获取list集合
		filmQueryVo.setCurrentPage(currentPage);
		filmQueryVo.setPageCount(pageSize);
		List<Film> filmList = filmMapper.selectFilmByCondition(filmQueryVo);
		PageBean<Film> pageBean = new PageBean<>(count, pageSize);
		pageBean.setCurrent(currentPage);
		pageBean.setDatas(filmList);
		return pageBean;
	}

}
